package com.yx.learn.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yx.learn.domain.Authority;
import com.yx.learn.service.AuthorityService;


@Controller("authorityAction")
@Scope("prototype")
public class AuthorityAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	@Resource
	AuthorityService authorityService;

	private int id;
	private int pid;
	private String authorityName;
	private String flag;
	private String url;
	private String memo;

	Authority authority = new Authority();
	
	/*
	 * treegrid请求数据
	 */
	public void getAll(){
		String aListJson;
		try {
			aListJson = authorityService.getAuthoritiesByIdJson(id).toString();
			this.toResponse(aListJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void save(){
		System.out.println("save");
		try {
			authority.setPid(pid);
			authority.setAuthorityName(authorityName);
			authority.setFlag(flag);
			authority.setUrl(url);
			authority.setMemo(memo);
			authorityService.saveAuthority(authority);
			this.toResponse("{\"status\":\"ok\" , \"message\":\"添加新权限成功!\"}");
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
			this.toResponse("{\"status\":\"fail\" , \"message\":\"添加新权限失败!\"}");
		}
	}
	
	public void update(){
		System.out.println("i am update"+authorityName);
		
		try {
			authority.setId(id);
			authority.setPid(pid);
			authority.setAuthorityName(authorityName);
			authority.setFlag(flag);
			authority.setUrl(url);
			authority.setMemo(memo);
			authorityService.updateAuthority(authority);
			this.toResponse("{\"status\":\"ok\" , \"message\":\"修改权限成功!\"}");
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
			this.toResponse("{\"status\":\"fail\" , \"message\":\"修改权限失败!\"}");
		}
	}
	
	public void delete(){
		try {
			authorityService.deleteAuthority(authorityService.findById(id));
			this.toResponse("ok");
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
			this.toResponse("fail");
		}
	}
	
	public void find(){
		System.out.println("i am find");
		String aListJson;
		try {
			aListJson = authorityService.getAuthoritiesByConditionJson(authorityName, flag).toString();
			this.toResponse(aListJson);
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}
	
	
	/*
	 * tree请求数据
	 */
	public void getAllForTree(){
		String aListJson;
		try {
			aListJson = authorityService.getAllAuthoritiesJson(id);
		System.out.println("aListJson"+aListJson);
			this.toResponse(aListJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	
	

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getAuthorityName() {
		return authorityName;
	}

	public void setAuthorityName(String authorityName) {
		this.authorityName = authorityName;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}
}
